#!/usr/bin/env bash

# Copyright 2021 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

################################################################################
# Checks if all bundled files are up to date. 
#
# Usage:
#     check/ts-build-changes
################################################################################

set -o errexit
set -o nounset

thisdir=$(dirname "${BASH_SOURCE[0]:?}")
repo_dir=$(git -C "${thisdir}" rev-parse --show-toplevel)
cd "${repo_dir}"

git_status=$(git status --short)
if [[ -n "${git_status}" ]]; then
    echo "$0 requires a pristine worktree, but 'git status' shows"
    echo "some changes or untracked files."
    echo
    echo "Please commit or clean these up to try again."
    exit 2
fi

echo "Removing generated bundle files.  If not restored by this script use"
echo 'git restore "*.bundle.js"    to recover them back.'
echo

git rm --quiet "cirq-web/*.bundle.js"
# restore deleted files in git index
git reset --quiet

echo "Building JavaScript bundle files"
echo

check/ts-build

git_status=$(git status --short)
if [[ -n "${git_status}" ]]; then
    echo
    echo -e "\033[31mERROR: check/ts-build changed generated files!\033[0m"
    echo -e "\033[31mPlease update and commit these files using check/ts-build\033[0m"
    echo
    echo "Output of 'git status':"
    echo
    git status
    exit 1
fi
